﻿// -----------------------------------------------------------------------
// <copyright file="CountryRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Linq;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CountryRepository : AbstractRepository<Country>
    {
        public CountryRepository()
        {
            base._contextHelper = null;
            base._context = new Context.AGRContext();
        }

        public override IQueryable<Country> GetAll()
        {
            var result = (from x in base._context.Countries
                          select new Country
                          {
                              Id = x.Id,
                              CountryCode = x.CountryCode,
                              CountryName = x.CountryName,
                              CountryLong = x.CountryLong,
                              Nationality = x.Nationality,
                              Displayed = x.Displayed,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Country entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Country entityToAdd = new Context.Country
            {
                CountryCode = entity.CountryCode,
                CountryName = entity.CountryName,
                CountryLong = entity.CountryLong,
                Nationality = entity.Nationality,
                Displayed = entity.Displayed,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Countries.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Country entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Country entityToUpdate = base._context.Countries.Single(x => x.CountryName.Equals(entity.CountryName));

            entityToUpdate.CountryCode = entity.CountryCode;
            entityToUpdate.CountryName = entity.CountryName;
            entityToUpdate.CountryLong = entity.CountryLong;
            entityToUpdate.Nationality = entity.Nationality;
            entityToUpdate.Displayed = entity.Displayed;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Country entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Country entityToDelete = base._context.Countries.Single(x => x.CountryName.Equals(entity.CountryName));

            base._context.Countries.DeleteOnSubmit(entityToDelete);
        }
    }
}
